<?php

class BlogController extends AuthController {
	private $DB;
	public function __Init(){
		$this->DB = new BlogModel;
	}
	public function index(){
		
		$this->display('main.html');
	}

	//添加栏目
	public function add(){
		if(ISPOST){
			if($this->DB->addBlog()){
				$this->success('添加成功',__CONTROLLER__.'&c=Blog&a=blogList');
			}else{
				$this->error($this->DB->error);
			}
		}else{
			//读取栏目
			$category = new CategoryModel;
			$categoryData= $category->getCategory();
			$this->assign('categoryData',$categoryData);
			$this->display('add.html');
		}
	}
	//修改Blog
	public function edit(){
		if(ISPOST){
			if($this->DB->editBlog()){
				$this->success('修改成功',__CONTROLLER__."&c=Blog&a=blogList");
			}else{
				$this->error($this->DB->error);
			}
		}else{
			//读取博客
			$bid= $_GET['bid'];
			$field = $this->DB->where('bid='.$bid)->find();
			//分配栏目
			$category = new CategoryModel;
			$categoryData= $category->getCategory();
			//当前博客栏目为选中状态
			foreach($categoryData as $id=>$cat){
				$categoryData[$id]['selected']=$cat['cid']==$field['cid']?" selected=''":'';
			}
			$this->assign('categoryData',$categoryData);
			$this->assign('field',$field);
			$this->display('edit.html');
		}
	}

	public function indexlist(){
		$data = $this->DB->getAll();
		$totalPage = $this->DB->count();
		$this->assign('data',$data['blog']);
		$this->assign('page',$data['page']);
		$this->assign('totalPage',$totalPage);
		$this->display('list.html');
	}

	public function blogList(){
		$data = $this->DB->getAll();
		$totalPage = $this->DB->count();
		$this->assign('data',$data['blog']);
		$this->assign('page',$data['page']);
		$this->assign('totalPage',$totalPage);
		$this->display('blogList.html');
	}

	public function delete(){
		if(isset($_GET['bid'])){
			$data = $this->DB->where("bid = {$_GET['bid']}")->find();

			if($data){
				$this->DB->where("bid = {$data['bid']} ")->deleteBlog($data);
				 $this->success('删除成功',__MODEL__.'&c=blog&a=blogList');
			}else{
				 $this->error('非法操作',__MODEL__);
			}
		}else{
			$this->error($this->DB->error);
		}
		

	}
}


?>